﻿using System;
using System.Collections;
using System.ComponentModel.Design;
using Microsoft.Practices.WizardFramework;
using Sixeyed.CodeGeneration.Metadata;

namespace Sixeyed.Guidance.CodeGeneration.CustomWizardPages
{
    /// <summary>
    /// Wizard page for selecting metadata items used in code generation
    /// </summary>
    public partial class SelectItems : CodeGenerationPageBase
    {
        /// <summary>
        /// Gets/sets selected metadata items
        /// </summary>
        [RecipeArgument]
        public ArrayList SelectedItems
        {
            get { return this.GetArgumentValue<ArrayList>("SelectedItems"); }
            set { this.SetArgumentValue<ArrayList>("SelectedItems", value); }
        }  

        /// <summary>
        /// Constructor with initial state
        /// </summary>
        /// <param name="parent">Parent wizard form</param>
        public SelectItems(WizardForm parent) : base(parent)
        {
            InitializeComponent();
            Metadata.SelectedProviderChanged += new Sixeyed.Guidance.CodeGeneration.CodeGeneration.Metadata.SelectedProviderChangedHandler(Metadata_SelectedProviderChanged);
            this.itemSelection1.ItemSelectionChanged += new Sixeyed.CodeGeneration.UI.ItemSelection.ItemSelectionChangedHandler(itemSelection1_ItemSelectionChanged);
        }

        /// <summary>
        /// Sets up the source and provider when the page is activated
        /// </summary>
        /// <returns>Whether successful</returns>
        public override bool OnActivate()
        {
            this.SetSource();
            this.SetProvider();
            return true;
        }

        /// <summary>
        /// Returns true if at least one item is selected
        /// </summary>
        public override bool IsDataValid
        {
            get
            {
                return (this.SelectedItems != null && this.SelectedItems.Count > 0);
            }
        }

        private void SetSource()
        {
            //if the metadata provider is null, this page has been loaded 
            //without SelectSource running - check SourceConfiguration is set:
            if (Metadata.Provider == null)
            {
                IDictionaryService dictionary = (IDictionaryService)this.Wizard.ServiceProvider.GetService(typeof(IDictionaryService));
                SourceConfiguration sourceConfiguration = dictionary.GetValue("SourceConfiguration") as SourceConfiguration;
                if (sourceConfiguration == null)
                {
                    throw new ArgumentNullException("SourceConfiguration");
                }
                Metadata.SetSource(sourceConfiguration);
            }
        }

        private void SetProvider()
        {
            this.itemSelection1.Provider = Metadata.Provider;
        }

        private void Metadata_SelectedProviderChanged(object sender)
        {
            SetProvider();
        }

        private void itemSelection1_ItemSelectionChanged(object sender)
        {
            ArrayList selectedItems = new ArrayList();
            selectedItems.AddRange(this.itemSelection1.GetSelectedItems().ToArray());
            this.SelectedItems = selectedItems;
            this.EnableNextStep();
        }      
    }
}